#!/bin/bash

source /works/deploy/sshlib

# 创建资源服务器
res() {
    cossh bn-py push srv_init
    cossh bn-py "sudo NAME=RES /tmp/srv_init res 16954; rm -f /tmp/srv_init"
}

push() {
    cossh bn-py push $@ /works/res/tang/
    cossh bn-py "chmod +r /works/res/tang -R"
}

create() {
    local cmd="cd /tmp;"
    local host="$1"
    local srvname="$2"

    test -z "$host" && err_exit "请指定服务器地址参数!" 1
    test -z "$srvname" && err_exit "请指定服务器名称参数!" 1

    shift 2

    cossh $host push ./srv_init

    cmd="cd /tmp;"
    # cmd+="curl -s -o srv_init http://${SRVLIST[aws-ad]#*@}:16954/srv_init;"
    cmd+="chmod +x srv_init;"
    cmd+="sudo NAME=${NAME-SRV} ./srv_init $srvname $@;"
    cmd+="rm -f srv_init"
    cossh $host "$cmd"
}

signel() {
    echo create "$@"
    create "$@"

    local name=${1%-*}
    test -z "$name" && return 2

    local host=${SRVLIST[$1]}
    test -z "$host" && return 3

    local ip=${host#*@}
    local user=${host%@*}

    cd /stride/admin
    echo KEYNAME=$name SRVUSER=$user SRVNAME=$ip ./container.sh release
    KEYNAME=$name SRVUSER=$user SRVNAME=$ip ./container.sh release

    cd /stride/pay-api
    echo KEYNAME=$name SRVUSER=$user SRVNAME=$ip ./container_run deploy
    KEYNAME=$name SRVUSER=$user SRVNAME=$ip ./container_run deploy

    cd /stride/notify-api
    echo KEYNAME=$name SRVUSER=$user SRVNAME=$ip ./container_run deploy
    KEYNAME=$name SRVUSER=$user SRVNAME=$ip ./container_run deploy

    cd /works/websocket
    echo KEYNAME=$name SRVUSER=$user SRVNAME=$ip ./container.sh release
    KEYNAME=$name SRVUSER=$user SRVNAME=$ip ./container.sh release
}

gw() {
    cossh tang-p1 "$@"
}

aws() {
    local name=${1-ad}
    shift
    gw "/works/cli aws $name $@"
}

aws-in() {
    local srv=${1-api}
    shift
    gw "/works/cli aws-in $srv $@"
}

bn() {
    cossh bnpy-ad 'ssh -i /works/JP.pem ubuntu@3.114.154.112'
}

cmd="${1-cossh}"
shift

$cmd "$@"

